//
//  ScratchableView.m
//  jiugonge
//
//  Created by 江湖 on 2018/5/7.
//  Copyright © 2018年 江湖. All rights reserved.
//

#import "ScratchableView.h"
@interface ScratchableView()
@property(nonatomic,assign)CGFloat heights;
@end
@implementation ScratchableView

/**
 九宫格布局
 @param direction 方向 0 横向 1 纵向
 */

/**
 九宫格布局

 @param direction 方向 0 横向 1 纵向
 @return 返回高度
 */
-(CGFloat)addScratchableLatex:(int)direction {
    NSInteger num = self.row;//数量
    NSInteger totalColumns = self.totalColumns;//行数或列数
    CGFloat totalX = self.totalX;//x间隙
    CGFloat totalY = self.totalY;//y间隙
    
    if (direction == 0) {
        CGFloat W = (self.frame.size.width-(totalColumns*totalX)-totalX)/totalColumns;
        CGFloat H = self.cellheight;
        for (int i = 0; i < num; i++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.tag = 10+i;
            button.backgroundColor = [UIColor cyanColor];
            int row = i /totalColumns;
            int col = i %totalColumns;
            CGFloat X = totalX + col *(W + totalX);
            CGFloat Y = totalY + row *(H + totalY);
            button.frame = CGRectMake(X, Y, W, H);
            [self addSubview:button];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            if ([self.scratchDelegate respondsToSelector:@selector(scratchableWithview:andindex:)]) {
                [self.scratchDelegate scratchableWithview:button andindex:i];
            }
            self.heights = self.heights+Y;
        }
    }else if (direction == 1){
        CGFloat H = (self.frame.size.height-(totalColumns*totalY)-totalY)/totalColumns;
        CGFloat W = self.cellheight;
        
        for (int i = 0; i < num; i++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.tag = 10+i;
            button.backgroundColor = [UIColor cyanColor];
            int row = i %totalColumns;
            int col = i /totalColumns;
            CGFloat X = totalX + col *(W + totalX);
            CGFloat Y = totalY + row *(H + totalY);
            button.frame = CGRectMake(X, Y, W, H);
            
            [self addSubview:button];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            
            if ([self.scratchDelegate respondsToSelector:@selector(scratchableWithview:andindex:)]) {
                [self.scratchDelegate scratchableWithview:button andindex:i];
            }
            self.heights = self.heights+Y;
        }
    }
    return self.heights;

}

/**
 设置属性

 @param row 数量
 @param cellHeight 高度
 @param totalColumns 行数或列数
 @param totalX x间隙
 @param totalY y间隙
 */
-(void)setRow:(NSInteger)row cellHeight:(CGFloat)cellHeight totalColumns:(NSInteger)totalColumns totalX:(CGFloat)totalX totalY:(CGFloat)totalY{
    self.row = row;
    self.cellheight= cellHeight;
    self.totalColumns = totalColumns;
    self.totalX = totalX;
    self.totalY = totalY;
}
-(void)buttonClick:(UIButton *)sender
{
    if ([self.scratchDelegate respondsToSelector:@selector(scratchableWithSelect:andindex:)]) {
        [self.scratchDelegate scratchableWithSelect:sender andindex:sender.tag-10];
    }
}
-(void)relouData{
    for (int i=0; i<self.row; i++) {
        UIButton *sender = (UIButton *)[self viewWithTag:10+i];
        [sender.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        if ([self.scratchDelegate respondsToSelector:@selector(scratchableWithview:andindex:)]) {
            [self.scratchDelegate scratchableWithview:sender andindex:i];
        }
    }
}
@end
